#include "hdcontroller.h"
#include <QDataStream>

HDController::HDController(QObject *parent, int id)
	: QObject(parent), _id(id)
{
	_position = new ThreeDimensionPoint();

	_thumbPressed = false;
	_indexPressed = false;
	_middlePressed = false;
	_ringPressed = false;
}

HDController::~HDController()
{

}

ThreeDimensionPoint* HDController::position()
{
	return _position;
}

void HDController::setPosition(ThreeDimensionPoint *pos)
{
	if(pos->x != _position->x || pos->y != _position->y || pos->z != _position->z)
	{
		*_position = *pos;
		emit controllerMoving(this);
	}
}

bool HDController::thumbPressed()
{
	return _thumbPressed;
}

void HDController::setThumbPressed(bool pressed)
{
	if(_thumbPressed != pressed)
	{
		_thumbPressed = pressed;
		
		if(pressed)
			emit controllerPressed(this);
		else
			emit controllerReleased(this);
	}
}

bool HDController::indexPressed()
{
	return _indexPressed;
}

void HDController::setIndexPressed(bool pressed)
{
	if(_indexPressed != pressed)
	{
		_indexPressed = pressed;
		
		if(pressed)
			emit controllerPressed(this);
		else
			emit controllerReleased(this);
	}
}

bool HDController::middlePressed()
{
	return _middlePressed;
}

void HDController::setMiddlePressed(bool pressed)
{
	if(_middlePressed != pressed)
	{
		_middlePressed = pressed;

		if(pressed)
			emit controllerPressed(this);
		else
			emit controllerReleased(this);
	}
}

bool HDController::ringPressed()
{
	return _ringPressed;
}

void HDController::setRingPressed(bool pressed)
{
	if(_ringPressed != pressed)
	{
		_ringPressed = pressed;

		if(pressed)
			emit controllerPressed(this);
		else
			emit controllerReleased(this);
	}
}

QDataStream& operator<<(QDataStream &s, HDController &controller)
{
	s<<controller._id;
	s<<controller._position->x<<controller._position->y<<controller._position->z;
	s<<controller._thumbPressed<<controller._indexPressed<<controller._middlePressed<<controller._ringPressed;

	return s;
}

QDataStream& operator>>(QDataStream &s, HDController &controller)
{
	float x, y, z;
	bool thumb, index, middle, ring;

	s>>controller._id;
	s>>x>>y>>z;
	s>>thumb>>index>>middle>>ring;

	ThreeDimensionPoint *pos = new ThreeDimensionPoint(x, y, z);
	controller.setPosition(pos);
	delete pos;

// 	controller.setThumbPressed(thumb);
// 	controller.setIndexPressed(index);
// 	controller.setMiddlePressed(middle);
// 	controller.setRingPressed(ring);

	return s;
}

int HDController::GetControllerId()
{
	return _id;
}